package com.wwz.yygh.hosp.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wwz.util.MD5;
import com.wwz.yygh.common.exception.YyghException;
import com.wwz.yygh.common.result.Result;
import com.wwz.yygh.hosp.HospitalSet;
import com.wwz.yygh.hosp.service.HospitalSetService;
import com.wwz.yygh.model.base.BaseEntity;
import com.wwz.yygh.model.vo.HospitalSetQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.Random;

@Slf4j
@RestController
@RequestMapping("admin/hosp/hospitalSet")
@Api(tags = "医院设置接口")
@CrossOrigin
public class HospitalSetController {

    private final HospitalSetService hospitalSetService;

    @Autowired
    public HospitalSetController(HospitalSetService hospitalSetService) {
        this.hospitalSetService = hospitalSetService;
    }

    @GetMapping("/findAll")
    @ApiOperation(value = "1.查询所有医院信息")
    public Result<List<HospitalSet>> findAllHospitalSet() {
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "2.删除医院信息")
    public Result<String> deleteData(@PathVariable Long id) {
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return Result.ok("删除成功");

        } else {
            return Result.fail("删除失败");
        }
    }


    @PostMapping("/toPage/{current}/{limit}")
    @ApiOperation(value = "3.分页条件查询")
    public Result toPage(@PathVariable long current,
                         @PathVariable long limit,
                         @RequestBody HospitalSetQuery query) {

        //创建分页对象
        Page<HospitalSet> page = new Page<>(current, limit);

        //判断条件
        LambdaQueryWrapper<HospitalSet> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(HospitalSet::getCreateTime);
        if (!StringUtils.isEmpty(query.getHosname())) {
            wrapper.like(HospitalSet::getHosname, query.getHosname());
        }
        if (!StringUtils.isEmpty(query.getHoscode())) {
            wrapper.eq(HospitalSet::getHoscode, query.getHoscode());
        }
        Page<HospitalSet> datas = hospitalSetService.page(page, wrapper);
        return Result.ok(datas);
    }

    @PostMapping("/add")
    @ApiOperation(value = "4.添加医院设置")
    public Result addHospSet(@RequestBody HospitalSet hospitalSet){
        if (StringUtils.isEmpty(hospitalSet.getHosname())){
            throw new YyghException("医院名称不能为空",200);
        }
        hospitalSet.setStatus(1);
        //加密
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));

        boolean save = hospitalSetService.save(hospitalSet);
        if (save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }


    @PostMapping("/upDate")
    @ApiOperation(value = "5.修改医院设置")
    public Result<String> addOrUpDate(@RequestBody HospitalSet hospitalSet) {
        if (StringUtils.isEmpty(hospitalSet.getHosname())){
            throw new YyghException("医院名称不能为空");
        }
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @GetMapping("/findOne/{id}")
    @ApiOperation(value = "6.根据id查询医院")
    public Result<HospitalSet> findOne(@PathVariable String id) {
        if (id == null) {
            throw new YyghException("id不能为空");
        }
        HospitalSet oneData = hospitalSetService.getById(id);
        return Result.ok(oneData);
    }

    @DeleteMapping("/batchDelete")
    @ApiOperation(value = "7.批量删除医院信息")
    public Result<String> deleteDataList(@RequestBody List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)){
            throw new YyghException("id不能为空");
        }
        hospitalSetService.removeByIds(idList);
        return Result.ok("删除成功");
    }

    @PutMapping("/isLock/{id}/{status}")
    @ApiOperation(value = "8.医院设置锁定和解锁")
    public Result<String> isLock(@PathVariable String id, @PathVariable Integer status) {

        HospitalSet oneData = hospitalSetService.getById(id);
        if (oneData == null) {
            return Result.fail("没有这条数据");
        }
        oneData.setStatus(status);
        hospitalSetService.updateById(oneData);
        return Result.ok();
    }

    @PutMapping("/sendKey/{id}")
    @ApiOperation(value = "9.发送签名密钥")
    public Result<String> sendKey(@PathVariable String id) {
        if (id == null) {
            throw new YyghException("id不能为空");
        }
        HospitalSet oneData = hospitalSetService.getById(id);
        return Result.ok("发送成功");
    }
}
